<?php

namespace app\admin\model;

use think\facade\Cache;
use think\facade\Log;
use think\Model;

class Cate extends Model
{
    protected $table = 'petct_hospital_cate';

    /**
     * 获取栏目数据
     * @return array
     */
    public function getCatesList()
    {
        try {

            $res = $this->field('id,classname,bclassid,add_time,sort,status')
                ->select()->toArray();
        }catch (\Exception $e) {

            return modelReMsg(-1, [], $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }

    /**
     * 获取栏目数据
     * @param $roleId
     * @return array
     */
    public function getCatesTree($roleId)
    {
        try {

            $result = $this->field('id,classname,bclassid')->select();
            $str = '';

            $roleModel = new Role();
            $rule = $roleModel->getRoleInfoById($roleId);

            $nodeArr = [];
            if(!empty($rule['data'])){
                $nodeArr = explode(',', $rule['data']['role_node']);
            }

            foreach($result as $key => $vo){
                $str .= '{ "id": "' . $vo['id'] . '", "pId":"' . $vo['bclassid'] . '", "name":"' . $vo['classname'].'"';

                if(!empty($nodeArr) && in_array($vo['id'], $nodeArr)){
                    $str .= ' ,"checked":1';
                }

                $str .= '},';

            }

            $res = '[' . rtrim($str, ',') . ']';

        }catch (\Exception $e) {

            return modelReMsg(-1, [], $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }

    /**
     * 根据栏目id获取栏目信息
     * @param $ids
     * @return array
     */
    public function getCateInfoByIds($ids)
    {
        try {

            $res = $this->whereIn('id', $ids)->select()->toArray();
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }

    /**
     * 添加栏目
     * @param $param
     * @return array
     */
    public function addCate($param)
    {
        try {

            // 检测唯一
            $has = $this->field('id')->where('classname', $param['classname'])
                ->where('bclassid', $param['bclassid'])->find();

            if (!empty($has)) {

                return modelReMsg(-2, '', '该栏目名称已经存在');
            }

            $param['add_time'] = strtotime(date('Y-m-d H:i:s'));

            $this->insert($param);
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '添加栏目成功');
    }

    /**
     * 编辑栏目
     * @param $param
     * @return array
     */
    public function editCate($param)
    {
        try {

            // 检测唯一
            $has = $this->field('id')->where('classname', $param['classname'])
                ->where('bclassid', $param['bclassid'])->where('id', '<>', $param['id'])->find();

            if (!empty($has)) {

                return modelReMsg(-2, '', '该栏目名称已经存在');
            }

            $this->where('id', $param['id'])->update($param);
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '编辑栏目成功');
    }

    /**
     * 根据id 获取栏目信息
     * @param $id
     * @return array
     */
    public function getCateInfoById($id)
    {
        try {

            $res = $this->where('id', $id)->find();

        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, ['classname' => ''], $e->getMessage());
        }

        return modelReMsg(0, $res, '编辑栏目成功');
    }

    /**
     * 根据id 删除栏目
     * @param $id
     * @return array
     */
    public function deleteCateById($id)
    {
        try {

            // 检测栏目下是否有其他的栏目
            $has = $this->where('bclassid', $id)->count();
            if ($has > 0) {
                return modelReMsg(-2, '', '该栏目下尚有其他栏目，不可删除');
            }

            $this->where('id', $id)->delete();

        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '删除栏目成功');
    }

    /**
     * 获取栏目的id
     * @param $path
     * @return array
     */
    public function getCateIdByPath($path)
    {
        try {

            $res = $this->field('id')->where('node_path', $path)->find();

        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, [], $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }

    /**
     * 获取角色菜单集合
     * @param $roleId
     * @return array
     */
    public function getRoleMenuMap($roleId)
    {
        try {
            $res = [];

            if (1 == $roleId) {

                $res = $this->field('node_id as id,node_name as title,node_pid as pid,node_path,node_icon')
                    ->where('is_menu', 2)->order('sort asc')->select()->toArray();
            } else {

                $roleModel = new Role();
                $roleInfo = $roleModel->getRoleInfoById($roleId)['data'];

                if (!empty($roleInfo)) {

                    $res = $this->field('node_id as id,node_name as title,node_pid as pid,node_path,node_icon')
                        ->whereIn('node_id', $roleInfo['role_node'])->where('is_menu', 2)->select()->toArray();
                }
            }
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, [], $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }
}